package com.digitalchina.lxnsy.activity.management.message;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.MessageDetailVO;
import com.digitalchina.lxnsy.entity.MsgListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management.message
 * 创建人：郑皓
 * 创建时间：2019/5/6 16:00
 * 修改备注：
 * ================================================
 */
public class DetailPesticideActivity extends BaseActivity {
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_land)
    TextView tvLand;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_num_area)
    TextView tvNumArea;
    @Bind(R.id.tv_dosage_unit)
    TextView tvDosageUnit;
    @Bind(R.id.tv_dosage)
    TextView tvDosage;
    @Bind(R.id.tv_member)
    TextView tvMember;
    @Bind(R.id.layout_tel)
    LinearLayout layoutTel;
    private String messageId = "";// msg ID
    private String tel = "";// 电话

    private MsgListVO vo;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_detail_pesticide);
    }

    @Override
    public void initMember() {
        vo = (MsgListVO) getIntent().getSerializableExtra("MsgListVO");
        messageId = vo.getMessageId();

        setTitle(vo.getMessageName());

        getMsgDetail();
    }

    @Override
    public void addListener() {
        layoutTel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtil.isStrEmpty(tel))
                    Utils.callPhone(activity, tel);
                else
                    showToast("获取中，请稍候重试");
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0: // 详情
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        MessageDetailVO msgVO = FastJsonUtil.getBean(object.toString(), "messageDetailVO", MessageDetailVO.class);

        if (msgVO != null) {
            // 日期
            tvDate.setText(msgVO.getCommonDate().substring(0, 10));
            // 地块
            tvLand.setText(msgVO.getLandParcelName());
            // 社员
            tvMember.setText(msgVO.getMember());
            // 亩数
            tvNumArea.setText(msgVO.getMuNumber());


            // 农药名称
            tvName.setText(msgVO.getPesticidesName());
            // 农药用量
            tvDosage.setText(msgVO.getPesticidedosage());

            tel = msgVO.getMobilePhone();
        }

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public void getMsgDetail() {

        /**
         * messageId	消息ID
         * userAccId	用户ID
         */
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", AdminXML.getUserAccId(activity));
        params.put("messageId", messageId);

        OkHttpUtil.post(activity, URL.URL_GET_MESSAGE_DETAIL, "正在加载", params, mHandler,
                MSG_S_0, MSG_F_0);
    }
}
